home *** CD-ROM | disk | FTP | other *** search
/ Power CD / Power CD ATARI-Rechner Lieben.iso / UTILITY / LSRC_222 / WILDCARD.C < prev   
Encoding:
C/C++ Source or Header  |  1993-07-28  |  1.7 KB  |  110 lines

  1.  
  2. #include "lharc.h"
  3.  
  4. int matchpattern(register uchar *s,register uchar *p)
  5. {
  6.     register uchar s_c,p_c;
  7.     register int matched,reverse;
  8.  
  9.     for (;(p_c=*p++)!='\0';)
  10.     {
  11.         s_c=*s++;
  12.  
  13.         switch (p_c)
  14.         {
  15.         case '@':
  16.             if (s_c!=*p++)
  17.                 return(FAULT);
  18.             break;
  19.         case '?':
  20.             if (s_c=='\0')
  21.                 return(FAULT);
  22.             break;
  23.         case '*':
  24.             if (s_c=='\0')
  25.                 return((*p=='\0') ? SUCCS :FAULT);
  26.             else
  27.             {
  28.                 if (*p!='\0')
  29.                 {
  30.                     s--;
  31.                     while (!matchpattern(s,p))
  32.                         if (*++s=='\0')
  33.                             return(FAULT);
  34.                 }
  35.                 return(SUCCS);
  36.             }
  37.         case '[':
  38.             matched=FAULT;
  39.  
  40.             if (*p=='^')
  41.             {
  42.                 reverse=SUCCS;
  43.                 p++;
  44.             }
  45.             else
  46.                 reverse=FAULT;
  47.  
  48.             for (; (p_c=*p++)!='\0' && p_c!=']';)
  49.             {
  50.                 if (p_c=='-')
  51.                 {
  52.                     if (s_c<=*p++ && s_c>=p[-3])
  53.                         matched=SUCCS;
  54.                 }
  55.                 else if (p_c=='@' && s_c==*p++)
  56.                     matched=SUCCS;
  57.                 else if (s_c==p_c && p_c!='|')
  58.                     matched=SUCCS;
  59.             }
  60.  
  61.             if (matched==reverse)
  62.                 return(FAULT);
  63.  
  64.             break;
  65.         default:
  66.             if (s_c!=p_c)
  67.                 return(FAULT);
  68.             break;
  69.         }
  70.     }
  71.  
  72.     return(*s=='\0');
  73. }
  74.  
  75. int chk_wild(uchar *fname,uchar *fspec,int Wild)
  76. {
  77.     if (*fspec)
  78.     {
  79.         register uchar *p=fspec;
  80.  
  81.         if (*p++=='*' && (*p=='\0' || (*p++=='.' && *p++=='*' && *p++=='\0')))
  82.             return(SUCCS);
  83.         else
  84.         {
  85.             register int res=0,len=(int) strlen(fspec);
  86.  
  87.             p=strtok(fspec,", ");
  88.             while (p)
  89.             {
  90.                 if (Wild)
  91.                     res=(!strcmp(fname,p));
  92.                 else
  93.                     res=matchpattern(fname,p);
  94.  
  95.                 if (res)
  96.                 {
  97.                     while (*p++);
  98.                     if ((long) ((--p)-fspec)<len)
  99.                         *p=',';
  100.                     return(SUCCS);
  101.                 }
  102.                 else if ((p=strtok(NULL,", "))!=NULL)
  103.                     p[-1]=',';
  104.             }
  105.         }
  106.     }
  107.  
  108.     return(FAULT);
  109. }
  110.